package com.orchid.community.models.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.orchid.community.common.exceptions.CustomException;
import com.orchid.community.common.utils.CodeEnum;
import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.models.DTO.ChannelListRequestDTO;
import com.orchid.community.models.DTO.ChannelRequestDTO;
import com.orchid.community.models.entity.Channel;
import com.orchid.community.models.mapper.SceneMapper;
import com.orchid.community.models.service.SceneService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SceneServiceImpl implements SceneService {

    @Autowired
    private SceneMapper sceneMapper;


    @Override
    public MsgResponse createChannel(ChannelRequestDTO requestDTO) {
        Channel channel = sceneMapper.getChannelByChannelNo(requestDTO.getChannelNo());
        if(channel != null){
            return ResultUtils.fail("场景编号已存在");
        }
        Channel newChannel = new Channel();
        BeanUtils.copyProperties(requestDTO,newChannel);
        newChannel.setIsDelete(0);
        sceneMapper.insertChannel(newChannel);
        return ResultUtils.success("OK");
    }

    @Override
    public int deleteChannel(Map<String, String> map) {
        Channel channel = sceneMapper.getChannelById(Integer.valueOf(map.get("id")));
        if (channel == null) {
            throw new CustomException(CodeEnum.DEL_FAIL);
        }
        channel.setIsDelete(1);
        return sceneMapper.updateChannel(channel);
    }

    @Override
    public MsgResponse updateChannel(ChannelRequestDTO requestDTO) {
        Channel channel = sceneMapper.getChannelById(requestDTO.getId());
        if (channel == null) {
            return ResultUtils.fail("场景不存在");
        }
        // 通过Channel_NO判断是否已经存在
        Channel channelNo = sceneMapper.getChannelByChannelNo(requestDTO.getChannelNo());
        if (channelNo != null
                && !channelNo.getId().equals(channel.getId())
                && channelNo.getChannelNo().equals(channel.getChannelNo())) {
            return ResultUtils.fail("标签重复");
        }
        Channel updateChannel = new Channel();
        BeanUtils.copyProperties(requestDTO, updateChannel);
        updateChannel.setIsDelete(channel.getIsDelete());
        sceneMapper.updateChannel(updateChannel);
        return ResultUtils.success("ok");
    }

    @Override
    public MsgResponse getChannelList(ChannelListRequestDTO requestDTO) {
        PageHelper.startPage(requestDTO.pageNo, requestDTO.pageSize);
        List<ChannelRequestDTO> pageList = sceneMapper.getChannelList(requestDTO.getParam());
        for (ChannelRequestDTO channelRequestDTO : pageList) {
            String tagss = channelRequestDTO.getTagss();
            if(tagss != null){
                String[] split = tagss.split(", ");
                channelRequestDTO.setTags(Arrays.asList(split));
            }else {
                channelRequestDTO.setTags(new ArrayList<>());
            }
        }
        PageInfo<ChannelRequestDTO> pageInfo = new PageInfo<>(pageList);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return ResultUtils.success(resultMap);
    }

    @Override
    public MsgResponse getAllCityList() {
        return ResultUtils.success(sceneMapper.getAllCityList());
    }

    @Override
    public MsgResponse getAllChannel() {
        return ResultUtils.success(sceneMapper.getAllChannel());
    }


}
